package com.waner.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.waner.reggie.common.CustomException;
import com.waner.reggie.dto.SetmealDto;
import com.waner.reggie.entity.Setmeal;
import com.waner.reggie.entity.SetmealDish;
import com.waner.reggie.mapper.SetmealMapper;
import com.waner.reggie.service.CategoryService;
import com.waner.reggie.service.SetmealDishService;
import com.waner.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 分页查询套餐并且返回套餐种类名称字段信息
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public Page<SetmealDto> pageWithCategoryName(int page, int pageSize, String name) {
        // 分页查询处理器  setmeal->setmealDto
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> pageRes = new Page<>();

        // 构造条件查询器
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(name),Setmeal::getName,name);
        queryWrapper.orderByDesc(Setmeal::getUpdateTime);

        // 开始进行分页查询
        this.page(pageInfo,queryWrapper);

        // 开始进行数据的补充
        BeanUtils.copyProperties(pageInfo,pageRes,"records");

        // 开始进行records的转化
        List<Setmeal> setmeals = pageInfo.getRecords();
        List<SetmealDto> setmealDtos = new LinkedList<>();
        for(Setmeal setmeal:setmeals){
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal,setmealDto);
            setmealDto.setCategoryName(categoryService.getById(setmeal.getCategoryId()).getName());
            setmealDtos.add(setmealDto);
        }
        pageRes.setRecords(setmealDtos);

        return pageRes;
    }

    /**
     * 新增套餐，以及套餐与菜品的关系、数据
     * @param setmealDto
     */
    @Override
    public void saveWithDishes(SetmealDto setmealDto) {
        // 新增套餐表的元数据
        this.save(setmealDto);

        // 更新套餐-菜品关系表
        List<SetmealDish> list = setmealDto.getSetmealDishes();
        list = list.stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(list);

    }

    /**
     * 批量删除套餐元素元数据以及套餐与菜品的映射关系
     * @param ids
     */
    @Override
    public void deleteWithDishes(List<Long> ids) {
        // 判断套餐是否能删除，不能删除则抛出业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,1);
        setmealLambdaQueryWrapper.in(Setmeal::getId,ids);

        if(this.count(setmealLambdaQueryWrapper)!=0){
            throw new CustomException("存在起售套餐，删除操作失败");
        }

        // 删除对应的套餐元数据
        this.removeByIds(ids);

        // 条件查询器  delete from setmeal_dish where setmeal_id in (..ids);
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetmealDish::getSetmealId,ids);

        // 根据套餐id删除setmealDish数据
        setmealDishService.remove(queryWrapper);
    }

    /**
     * 根据
     * @param id
     */
    @Override
    public SetmealDto getWithdishedById(Long id) {
        // 根据id查询对应的套餐元数据
        Setmeal setmeal = this.getById(id);

        // 构造条件查询器
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);

        List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);

        // 进行数据复制
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setSetmealDishes(setmealDishes);

        return setmealDto;
    }

    /**
     * 修改套餐得信息，以及相关的菜品信息
     * @param setmealDto
     */
    @Override
    public void updateWithDishes(SetmealDto setmealDto) {
        // 根据id更新套餐
        this.updateById(setmealDto);

        // 构造条件查询器
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        // 根据套餐id删除所有相关的菜品映射
        setmealDishService.remove(queryWrapper);

        // 新增新的菜品映射
        List<SetmealDish> list = setmealDto.getSetmealDishes();
        list = list.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(list);
    }
}
